/**
 * This project create for studying propose at BUPT University. You can change or reuse this project anywhere
 */
package edu.bupt.ooad.library.ui;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

public class AddTeacherDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    
    private JTextField firstnameTextField;
    
    private JTextField lastnameTextField;
    
    private JTextField departmentTextField;
    
    public String firstname;
    
    public String lastname;
    
    public String department;

    /**
     * Create the dialog.
     */
    public AddTeacherDialog(Dialog parent) {
        super(parent, true);
        setTitle("Add teacher ");
        
        setBounds(100, 100, 420, 182);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new MigLayout("", "[][grow]", "[][][]"));
        {
            JLabel label1 = new JLabel("Firstname:");
            contentPanel.add(label1, "cell 0 0,alignx trailing");
        }
        {
            firstnameTextField = new JTextField();
            contentPanel.add(firstnameTextField, "cell 1 0,growx");
            firstnameTextField.setColumns(10);
        }
        {
            JLabel label2 = new JLabel("Lastname:");
            contentPanel.add(label2, "cell 0 1,alignx trailing");
        }
        {
            lastnameTextField = new JTextField();
            contentPanel.add(lastnameTextField, "cell 1 1,growx");
            lastnameTextField.setColumns(10);
        }
        {
            JLabel label3 = new JLabel("Department:");
            contentPanel.add(label3, "cell 0 2,alignx trailing");
        }
        {
            departmentTextField = new JTextField();
            contentPanel.add(departmentTextField, "cell 1 2,growx");
            departmentTextField.setColumns(10);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg) {
                        add();
                    }
                });
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg) {
                        setVisible(false);
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

    private void add() {
        firstname = firstnameTextField.getText();
        lastname  = lastnameTextField.getText();
        department = departmentTextField.getText();
        
        if(firstname == null || firstname.trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Please enter valid firstname");
            return;
        }
        
        if(lastname == null || lastname.trim().length() == 0) {
            JOptionPane.showMessageDialog(this, "Please enter valid lastname");
            return;
        }
            
        setVisible(false);
    }
}
